import dTalkApi from '../core/dTalkApi';
import storeInfoModel from '../model/storeInfoModel';
import storeRecoderModel from '../model/storeRecoderModel';
import visitMonthPlanModel from '../model/visitMonthPlanModel';
import consumerRecoderModel from '../model/consumerRecoderModel';
import * as moment from 'moment';
import dTalkApiUtil from 'dingtalk-app/dtalk/dTalkApiUtil';
import userCheckInModel from '../model/userCheckInModel';
import formDataService from './formDataService';
import emplModel from '../model/empl2Model';
//报表service------统计各种数据
class ReportFormService {
    constructor() { }
    
    //(一)客户拜访达成率---报表
    async getReachConsumerRate(startDate1: string, endDate1: string, character: string, comment: string, leaderId: string) {
        let dateData=await formDataService.getDate(startDate1,endDate1)
        let startDate = dateData.startDate;
        let endDate= dateData.endDate;
        let month = moment(startDate).format('MM');
        let num3 =parseInt( moment(startDate).format('DD'));
        let num4 =parseInt( moment(endDate).format('DD'));
        let dateList=await formDataService.getDate2(num3,num4,startDate)//日期
        let access_token=await dTalkApiUtil.getAccessToken();
        let authority=await formDataService.getAuthrity(leaderId,access_token)//权限
        let groupName=authority.groupName;
        let leaderDepts = authority.leaderDepts
        let dataCache: any = []
        let list:any[]=[]
        let userId:any = '';
        let arr:any=[]
       
        let data5=await emplModel.find({})
        if(data5.length>0){
            for (let index5 = 0; index5 < data5.length; index5++) {
                let element = data5[index5];
                arr.push(element.userId)
            }
        }
        if (groupName.includes('销售')) {//销售
            if (comment=='个人') {
                if(character==''){
                    userId = leaderId;
                }else{
                   userId=character;
                }
                let userInfo = await dTalkApi.getUserDetail(userId,access_token);
                let name:any='';
                if (userInfo&&userInfo.name) {
                    name=userInfo.name
                } else {
                    let data5=await visitMonthPlanModel.findOne({userId:userId,'$and':[{user:{$ne:null}},{user:{$ne:''}}]})
                    if(data5){
                        name=data5.user
                    }
                }
                if(userInfo !=''&& userInfo !=null){
                    if (dateList.length > 0) {
                        for (let index = 0; index < dateList.length; index++) {
                            let date = dateList[index];
                            let consumerRate_:any = await formDataService.getConsumerRate_one_employeeDate1ToDate2(userId, date, date);
                            let consumerRate = (consumerRate_.toFixed(2)-0)*100;
                            dataCache.push({
                                comment: '一天',
                                userId: userId,
                                userName: name,
                                date:date,
                                consumerRate: consumerRate.toFixed(0)
                            })
                        }
                    }
                }
            }
        }else{
            let userList: any[] = [];
            if(leaderDepts.length>0){
                let sonDept:any=[]
                let sonData=await formDataService.getsonDept(leaderDepts,true,access_token);
                let userList2=await formDataService.newArray(sonData.userIdData)
                let son_list2=sonData.list;
               
                if (comment == '个人') {//已经做好
                    if(character !=''){
                       userId = character;
                        let userInfo = await dTalkApi.getUserDetail(userId,access_token);
                        let name:any='';
                        if (userInfo&&userInfo.name) {
                            name=userInfo.name
                        } else {
                            let data5=await visitMonthPlanModel.findOne({userId:userId,'$and':[{user:{$ne:null}},{user:{$ne:''}}]})
                            if(data5){
                                name=data5.user
                            }
                        }
                        if (dateList.length > 0) {
                            for (let index = 0; index < dateList.length; index++) {
                                let date = dateList[index];
                                let consumerRate_:any = await formDataService.getConsumerRate_one_employeeDate1ToDate2(userId, date, date);
                                let consumerRate = (consumerRate_.toFixed(2)-0)*100;
                                dataCache.push({
                                    comment: '一天',
                                    userId: userId,
                                    userName: name,
                                    date:date,
                                    consumerRate: consumerRate.toFixed(0)
                                })
                            }
            
                        }
                    }else{
                       
                        if(userList2.length>0){
                            for (let index = 0; index < userList2.length; index++) {
                                let element2 = userList2[index];
                                if(!arr.includes(element2)){
                                    continue;
                                }
                                let emp=await visitMonthPlanModel.findOne({userId:element2,"$and":[{user:{$ne:''}},{user:{$ne:null}}]})
                                if (!dataCache[emp.user]) {
                                    dataCache[emp.user] = {
                                        areaName:emp.user,
                                        consumerRate:''
                                    }
                                }
                                let consumerRate_one_employee = await formDataService.getConsumerRate_one_employeeDate1ToDate2(element2, startDate, endDate);
                                let rate:any=0;
                                rate=consumerRate_one_employee
                                let a=(rate.toFixed(2)-0)*100
                                dataCache[emp.user]={
                                    areaName:emp.user,
                                    consumerRate:a.toFixed(0)
                                }
                            }
                        }
                    }
                }else if (comment == '城市类型') {
                    let cityTypeList=await consumerRecoderModel.distinct("cityType",{comment:1,"$and":[{cityType:{$ne:''}},{cityType:{$ne:null}}]})
                    if(cityTypeList.length>0){
                        for (let cityindex = 0; cityindex < cityTypeList.length; cityindex++) {
                            let element = cityTypeList[cityindex];
                            if (!dataCache[element]) {
                                dataCache[element] = {
                                    areaName:element,
                                    consumerRate:''
                                }
                            }
                            userList=await consumerRecoderModel.distinct("userid",{cityType:element,comment:1,userid:{$in:userList2}})
                            let consumerRate_area=0;//客户拜访成功率
                            for (let userindex = 0; userindex < userList.length; userindex++) {
                                let element2 = userList[userindex];
                                let consumerRate_one_employee = await formDataService.getConsumerRate_one_employeeDate1ToDate2(element2, startDate, endDate);
                                consumerRate_area+=consumerRate_one_employee;
                            }
                            let rate:any=0;
                            if(userList.length>0){
                                rate=(consumerRate_area/userList.length);
                            }else{
                                rate=0
                            }
                            let a=(rate.toFixed(2)-0)*100;
                            dataCache[element]={
                                areaName:element,
                                consumerRate:a.toFixed(0)
                            }
                        }
                    }
                }else if (comment == '客户类型1') {
                    let typeList=await consumerRecoderModel.distinct("typeOne",{comment:1,"$and":[{typeOne:{$ne:null}},{typeOne:{$ne:''}}]})
                    // console.log(typeList,'<-------------typeList')
                    // let str:any=data.customerType
                    let patt1 = new RegExp(/\s+/g);  
                    if(typeList.length>0){
                        for (let index1 = 0; index1 < typeList.length; index1++) {
                            let element = typeList[index1];
                            // let patt1 = new RegExp(/\s+/g);  
                            if (patt1.test(element)) {  
                                console.log("有空格");  
                                let d=element.replace(new RegExp(' ',"gm"), '')
                                if(!d){
                                    continue;
                                }
                            } 
                            if (!dataCache[element]) {
                                dataCache[element] = {
                                    areaName:element,
                                    consumerRate:''
                                }
                            }
                            userList = await consumerRecoderModel.distinct("userid", { typeOne: element, comment: 1,"$and":[{userid:{$in:userList2}},{userid:{$ne:''}},{userid:{$ne:null}}]});
                            let consumerRate_area=0;//客户拜访成功率
                            for (let userindex = 0; userindex < userList.length; userindex++) {
                                let element2 = userList[userindex];
                                let consumerRate_one_employee = await formDataService.getConsumerRate_one_employeeDate1ToDate2(element2, startDate, endDate);
                                consumerRate_area+=consumerRate_one_employee;
                            }
                            let rate:any=0;
                            if(userList.length>0){
                                rate=(consumerRate_area/userList.length);
                            }else{
                                rate=0
                            }
                            let a=(rate.toFixed(2)-0)*100
                            dataCache[element]={
                                areaName:element,
                                consumerRate:a.toFixed(0)
                            }
                        }
                    }
                }else if (comment == '客户类型2') {
                    let typeList=await consumerRecoderModel.distinct("typeTwo",{comment:1,"$and":[{typeTwo:{$ne:''}},{typeTwo:{$ne:null}}]})
                    let patt1 = new RegExp(/\s+/g);  
                    if(typeList.length>0){
                        for (let index1 = 0; index1 < typeList.length; index1++) {
                            let element = typeList[index1];
                           
                            if (patt1.test(element)) {  
                                console.log("有空格");  
                                let d=element.replace(new RegExp(' ',"gm"), '')
                                if(!d){
                                    continue;
                                }
                            } 
                            if (!dataCache[element]) {
                                dataCache[element] = {
                                    areaName:element,
                                    consumerRate:''
                                }
                            }
                            userList = await consumerRecoderModel.distinct("userid", { typeTwo: element, comment: 1,"$and":[{userid:{$in:userList2}},{userid:{$ne:''}},{userid:{$ne:null}}]});
                            let consumerRate_area=0;//客户拜访成功率
                            for (let userindex = 0; userindex < userList.length; userindex++) {
                                let element2 = userList[userindex];
                                let consumerRate_one_employee = await formDataService.getConsumerRate_one_employeeDate1ToDate2(element2,startDate, endDate);
                                consumerRate_area+=consumerRate_one_employee;
                            }
                            let rate:any=0;
                            if(userList.length>0){
                                rate=(consumerRate_area/userList.length);
                            }else{
                                rate=0
                            }
                            let a=(rate.toFixed(2)-0)*100
                            dataCache[element]={
                                areaName:element,
                                consumerRate:a.toFixed(0)
                            }
                        }
                    }
                }else if (comment == '客户类型3') {
                    let typeList=await consumerRecoderModel.distinct("typeThree",{comment:1,"$and":[{typeThree:{$ne:''}},{typeThree:{$ne:null}}]})
                    let patt1 = new RegExp(/\s+/g);  
                    if(typeList.length>0){
                        for (let index1 = 0; index1 < typeList.length; index1++) {
                            let element = typeList[index1];
                            let patt1 = new RegExp(/\s+/g);  
                            if (patt1.test(element)) {  
                                console.log("有空格");  
                                let d=element.replace(new RegExp(' ',"gm"), '')
                                if(!d){
                                    continue;
                                }
                            } 
                            if (!dataCache[element]) {
                                dataCache[element] = {
                                    areaName:element,
                                    consumerRate:''
                                }
                            }
                            userList = await consumerRecoderModel.distinct("userid", { typeThree: element, comment: 1,"$and":[{userid:{$in:userList2}},{userid:{$ne:''}},{userid:{$ne:null}}]});
                            let consumerRate_area=0;//客户拜访成功率
                            for (let userindex = 0; userindex < userList.length; userindex++) {
                                let element2 = userList[userindex];
                                let consumerRate_one_employee = await formDataService.getConsumerRate_one_employeeDate1ToDate2(element2,startDate, endDate);
                                consumerRate_area+=consumerRate_one_employee;
                            }
                            let rate:any=0;
                            if(userList.length>0){
                                rate=(consumerRate_area/userList.length);
                            }else{
                                rate=0
                            }
                            let a=(rate.toFixed(2)-0)*100
                            dataCache[element]={
                                areaName:element,
                                consumerRate:a.toFixed(0)
                            }
                        }
                    }
                }else{
                    let sonData2:any={}
                    if(groupName.includes('区域')||groupName.includes('模块')){
                        sonData2=await formDataService.getsonDept(leaderDepts,false,access_token)
                    }
                    if (groupName.includes('区域')) {//区域负责人
                        if (comment == '省份') {//已经做完------未测试
                            sonDept=await formDataService.newArray(sonData2.listNames)
                            if(sonDept.length>0){
                                for (let sonDeptindex = 0; sonDeptindex < sonDept.length; sonDeptindex++) {
                                    let pro = sonDept[sonDeptindex];
                                    if (!dataCache[pro]) {
                                        dataCache[pro] = {
                                            areaName:pro,
                                            consumerRate:''
                                        }
                                    }
                                    userList = await visitMonthPlanModel.distinct("userId", { provinceName:pro, deptId: { $in:son_list2 }, userId: { $ne: '' } }); 
                                    let consumerRate_area=0;//一个区域的客户拜访成功率
                                    for (let userindex = 0; userindex < userList.length; userindex++) {
                                        let element2 = userList[userindex];
                                        let consumerRate_one_employee = await formDataService.getConsumerRate_one_employeeDate1ToDate2(element2, startDate, endDate);
                                        consumerRate_area+=consumerRate_one_employee;
                                    }
                                    let rate:any=0;
                                    if(userList.length>0){
                                        rate=(consumerRate_area/userList.length);
                                    }else{
                                        rate=0
                                    }
                                    let a=(rate.toFixed(2)-0)*100
                                    dataCache[pro]={
                                        areaName:pro,
                                        consumerRate:a.toFixed(0)
                                    }
                                }
                            }
                        }        
                    }
                    if(groupName.includes('全国')||groupName.includes('模块')){
                        let sonDept:any=[]
                        let son_area_list:any[]=[]
                        let son_area_list2:any[]=[]
                        let son_province_list:any[]=[]
                        if(groupName.includes('模块')){
                            if(sonData2.list.length==0){
                                son_area_list=[];
                                son_province_list=[];
                            }else if(sonData.list.length==sonData2.list.length&&sonData2.list.length>0){
                                son_province_list=[];
                                son_area_list=sonData2.listNames;
                                son_area_list2=sonData2.dept;
                            }else if(sonData.list.length !=sonData2.list.length&&sonData2.list.length>0){
                                son_area_list=sonData2.listNames;
                                for (let index2 = 0; index2 <sonData.listNames.length; index2++) {
                                    let element = sonData.listNames[index2];
                                    if(sonData2.listNames.includes(element)){
                                        son_province_list.push('')
                                    }else{
                                        son_province_list.push(element)
                                    }
                                }
                            }
                        }
                        if (comment== '区域') {//已经做好
                            let areaNameList:any[]=[]
                            if(groupName.includes('模块')){
                                areaNameList= son_area_list
                            }else if(groupName.includes('全国')){
                                areaNameList=await visitMonthPlanModel.distinct("areaName",{comment:1,"$and":[{areaName:{$ne:null}},{areaName:{$ne:''}}]})
                            }
                            if(areaNameList.length>0){
                                for (let index = 0; index < areaNameList.length; index++) {
                                    const element = areaNameList[index];
                                    if (!dataCache[element]) {
                                        dataCache[element] = {
                                            areaName:element,
                                            consumerRate:''
                                        }
                                    }
                                   /*  let areaId=await visitMonthPlanModel.findOne({areaName:element,"$and":[{area:{$ne:null}},{area:{$ne:''}}]})
                                    console.log(areaId.area,'<========',element);
                                    let area_id:any=''
                                    if(!areaId){
                                        // console.log('11111111111111111111111')
                                        for (let index2 = 0; index2 < son_area_list2.length; index2++) {
                                            let element2 = son_area_list2[index2];
                                            if(element2.name==element){
                                                area_id=element2.id  
                                            }
                                        }
                                    } */
                                    // let sonData2 = await dTalkApi.getSondepIdList(area_id, true);
                                    // let son_list3 = sonData2.department;
                                    // son_list3.push(parseInt(area_id));//区域id列表
                                    // userList = await visitMonthPlanModel.distinct("userId", {areaName:element, deptId: { $in: son_list3 },"$and":[{userId: { $ne: '' }},{userId: { $ne:null}}] });
                                    userList = await visitMonthPlanModel.distinct("userId", {areaName:element, deptId: { $in: son_list2 },"$and":[{userId: { $ne: '' }},{userId: { $ne:null}}] });
                                    let consumerRate_area=0;//一个区域的客户拜访成功率
                                    for (let userindex = 0; userindex < userList.length; userindex++) {
                                        let element2 = userList[userindex];
                                        let consumerRate_one_employee = await formDataService.getConsumerRate_one_employeeDate1ToDate2(element2, startDate, endDate);
                                        consumerRate_area+=consumerRate_one_employee;
                                    }
                                    let rate:any=0;
                                    if(userList.length>0){
                                        rate=(consumerRate_area/userList.length);
                                    }else{
                                        rate=0
                                    }
                                    let a=(rate.toFixed(2)-0)*100
                                    dataCache[element]={
                                        areaName:element,
                                        consumerRate:a.toFixed(0)
                                    }
                                }
                            }
                        }
                        if (comment == '省份') {//已经做完------未测试
                            let provinceNameList:any[]=[]
                            if(groupName.includes('模块')){
                                if(son_province_list.length>0){
                                    for (let proindex = 0; proindex < son_province_list.length; proindex++) {
                                        let element2 = son_province_list[proindex];
                                        if(element2){
                                            provinceNameList.push(element2)
                                        }
                                    }
                                }
                            }else if(groupName.includes('全国')){
                                provinceNameList=await visitMonthPlanModel.distinct("provinceName",{comment:1,"$and":[{provinceName:{$ne:null}},{provinceName:{$ne:''}}]})
                            }
                            if(provinceNameList.length>0){
                                for (let sonDeptindex = 0; sonDeptindex < provinceNameList.length; sonDeptindex++) {
                                    let pro = provinceNameList[sonDeptindex];
                                    if (!dataCache[pro]) {
                                        dataCache[pro] = {
                                            areaName:pro,
                                            consumerRate:''
                                        }
                                    }
                                    userList = await visitMonthPlanModel.distinct("userId", { provinceName:pro, comment: 1, deptId: { $in:son_list2 }, userId: { $ne: '' } }); 
                                    let consumerRate_area=0;//一个区域的客户拜访成功率
                                    for (let userindex = 0; userindex < userList.length; userindex++) {
                                        let element2 = userList[userindex];
                                        let consumerRate_one_employee = await formDataService.getConsumerRate_one_employeeDate1ToDate2(element2, startDate, endDate);
                                        consumerRate_area+=consumerRate_one_employee;
                                    }
                                    let rate:any=0;
                                    if(userList.length>0){
                                        rate=(consumerRate_area/userList.length);
                                    }else{
                                        rate=0
                                    }
                                    let a=(rate.toFixed(2)-0)*100
                                    dataCache[pro]={
                                        areaName:pro,
                                        consumerRate:a.toFixed(0)
                                    }
                                }
                            }
                        }
                    } 
                }
            }
        } 
        for (const key in dataCache) { //移植dataCache当中的属性到list当中
            if (dataCache.hasOwnProperty(key)) {
                const element = dataCache[key];
                list.push(element)
            }
        }
        if(list.length==0){
            list.push({
                areaName:'',
                consumerRate:0
            })
        }
        console.log(list,'list_list_list_list_list_list_list_list_客户拜访达成率')
        return list;
    }

    //(二)新客户拜访达成率----报表
    async getReach_NewConsumerRate(startDate1: string, endDate1: string, character: string, comment: string, leaderId: string) {
        let list: any[] = [];
        let dateData=await formDataService.getDate(startDate1,endDate1)
        let date1 = dateData.startDate;
        let date2= dateData.endDate;
        let month = moment(date1).format('MM');
        let num3 =parseInt( moment(date1).format('DD'));
        let num4 =parseInt( moment(date2).format('DD'));
        let dateList=await formDataService.getDate2(num3,num4,date1) //获取日期
        let access_token=await dTalkApiUtil.getAccessToken();
        let authority=await formDataService.getAuthrity(leaderId,access_token)  //权限问题
        let groupName=authority.groupName;
        let leaderDepts = authority.leaderDepts
        let userId:any = '';
        let dataCache: any = []
        let arr:any=[]
       
        let data5=await emplModel.find({})
        if(data5.length>0){
            for (let index5 = 0; index5 < data5.length; index5++) {
                let element = data5[index5];
                arr.push(element.userId)
            }
        }
        if (groupName.includes('销售')) {//销售
            if (comment=='个人') {
                if(character==''){
                    userId = leaderId;
                }else{
                   userId=character;
                }
                let userInfo = await dTalkApi.getUserDetail(userId,access_token);
                let name:any='';
                if (userInfo&&userInfo.name) {
                    name=userInfo.name
                } else {
                    let data5=await visitMonthPlanModel.findOne({userId:userId,'$and':[{user:{$ne:null}},{user:{$ne:''}}]})
                    if(data5){
                        name=data5.user
                    }
                }
                if (dateList.length > 0) {
                    for (let index = 0; index < dateList.length; index++) {
                        let date = dateList[index];
                        let new_consumerRate :any= await formDataService.getRate_NewConsumer_one_employeeDate1ToDate2(userId, date, date);
                       let a= (new_consumerRate.toFixed(2)-0)*100
                        dataCache.push({
                            comment: '一天',
                             userId: userId,
                            userName: name,
                            date: date,
                            newconsumerRate:a.toFixed(0)
                        })
                    }
                } 
            }
        }else{
            let userList: any[] = [];
            if(leaderDepts.length>0){
                let sonDept:any=[]
                let sonData=await formDataService.getsonDept(leaderDepts,true,access_token);
                let userList2=await formDataService.newArray(sonData.userIdData)
                let son_list2=sonData.list;
                if (comment == '个人') {
                    if(character !=''){
                       userId = character;
                        let userInfo = await dTalkApi.getUserDetail(userId,access_token);
                        let name:any='';
                        if (userInfo&&userInfo.name) {
                            name=userInfo.name
                        } else {
                            let data5=await visitMonthPlanModel.findOne({userId:userId,'$and':[{user:{$ne:null}},{user:{$ne:''}}]})
                            if(data5){
                                name=data5.user
                            }
                        }
                        if (dateList.length > 0) {
                            for (let index = 0; index < dateList.length; index++) {
                                let date = dateList[index];
                                let new_consumerRate :any= await formDataService.getRate_NewConsumer_one_employeeDate1ToDate2(userId, date, date);
                               let a=(new_consumerRate.toFixed(2)-0)*100
                                dataCache.push({
                                    comment: '一天',
                                    userId: userId,
                                    userName:name,
                                    date: date,
                                    newconsumerRate: a.toFixed(0)
                                })
                            }
                        }
                    }else{
                        if(userList2.length>0){
                            for (let index = 0; index < userList2.length; index++) {
                                let element2 = userList2[index];
                                if(!arr.includes(element2)){
                                    continue;
                                }
                                let emp=await visitMonthPlanModel.findOne({userId:element2,"$and":[{user:{$ne:''}},{user:{$ne:null}}]})
                                if (!dataCache[emp.user]) {
                                    dataCache[emp.user] = {
                                        areaName:emp.user,
                                        newconsumerRate:''
                                    }
                                }
                                let consumerRate_one_employee = await formDataService.getRate_NewConsumer_one_employeeDate1ToDate2(element2, date1, date2);
                                let rate:any=consumerRate_one_employee
                                let a=(rate.toFixed(2)-0)*100
                                dataCache[emp.user]={
                                    areaName:emp.user,
                                    newconsumerRate:a.toFixed(0)
                                }
                            }
                        }
                       
                    }
                }else if (comment == '城市类型') {
                    let cityTypeList=await consumerRecoderModel.distinct("cityType",{comment:1,"$and":[{cityType:{$ne:null}},{cityType:{$ne:''}}]})
                    if(cityTypeList.length>0){
                        for (let cityindex = 0; cityindex < cityTypeList.length; cityindex++) {
                            let element = cityTypeList[cityindex];
                            if (!dataCache[element]) {
                                dataCache[element] = {
                                    areaName:element,
                                    newconsumerRate:''
                                }
                            }
                            userList=await consumerRecoderModel.distinct("userid",{cityType:element,comment:1,userid:{$in:userList2}})
                            let consumerRate_area=0;//客户拜访成功率
                            for (let userindex = 0; userindex < userList.length; userindex++) {
                                let element2 = userList[userindex];
                                let consumerRate_one_employee = await formDataService.getRate_NewConsumer_one_employeeDate1ToDate2(element2, date1, date2);
                                consumerRate_area+=consumerRate_one_employee;
                            }
                            let rate:any=0;
                            if(userList.length>0){
                                rate=(consumerRate_area/userList.length);
                            }else{
                                rate=0
                            }
                            let a=(rate.toFixed(2)-0)*100
                            dataCache[element]={
                                areaName:element,
                                newconsumerRate:a.toFixed(0)
                            }
                        }
                    }
                }else if (comment == '客户类型1') {
                    let typeList=await consumerRecoderModel.distinct("typeOne",{comment:1,"$and":[{typeOne:{$ne:null}},{typeOne:{$ne:''}}]})
                    let patt1 = new RegExp(/\s+/g);  
                    if(typeList.length>0){
                        for (let index1 = 0; index1 < typeList.length; index1++) {
                            let element = typeList[index1];
                            if (patt1.test(element)) {  
                                console.log("有空格");  
                                let d=element.replace(new RegExp(' ',"gm"), '')
                                if(!d){
                                    continue;
                                }
                            } 
                            if (!dataCache[element]) {
                                dataCache[element] = {
                                    areaName:element,
                                    newconsumerRate:''
                                }
                            }
                            userList = await consumerRecoderModel.distinct("userid", { typeOne: element, comment: 1,"$and":[{userid:{$in:userList2}},{userid:{$ne:null}}]});
                            let consumerRate_area=0;//客户拜访成功率
                            for (let userindex = 0; userindex < userList.length; userindex++) {
                                let element2 = userList[userindex];
                                let consumerRate_one_employee = await formDataService.getRate_NewConsumer_one_employeeDate1ToDate2(element2, date1, date2);
                                consumerRate_area+=consumerRate_one_employee;
                            }
                            let rate:any=0;
                            if(userList.length>0){
                                rate=(consumerRate_area/userList.length);
                            }else{
                                rate=0
                            }
                            let a=(rate.toFixed(2)-0)*100
                            dataCache[element]={
                                areaName:element,
                                newconsumerRate:a.toFixed(0)
                            }
                        }
                    }
                }else if (comment == '客户类型2') {
                    let typeList=await consumerRecoderModel.distinct("typeTwo",{comment:1,"$and":[{typeTwo:{$ne:null}},{typeTwo:{$ne:''}}]})
                    let patt1 = new RegExp(/\s+/g);  
                    if(typeList.length>0){
                        for (let index1 = 0; index1 < typeList.length; index1++) {
                            let element = typeList[index1];
                          
                            if (patt1.test(element)) {  
                                console.log("有空格");  
                                let d=element.replace(new RegExp(' ',"gm"), '')
                                if(!d){
                                    continue;
                                }
                            } 
                            if (!dataCache[element]) {
                                dataCache[element] = {
                                    areaName:element,
                                    newconsumerRate:''
                                }
                            }
                            userList = await consumerRecoderModel.distinct("userid", { typeTwo: element, comment: 1,"$and":[{userid:{$in:userList2}},{userid:{$ne:null}}]});
                            let newconsumerRate_area=0;//客户拜访成功率
                            for (let userindex = 0; userindex < userList.length; userindex++) {
                                let element2 = userList[userindex];
                                let newconsumerRate_one_employee = await formDataService.getRate_NewConsumer_one_employeeDate1ToDate2(element2, date1, date2);
                                newconsumerRate_area+=newconsumerRate_one_employee;
                            }
                            let rate:any=0;
                            if(userList.length>0){
                                rate=(newconsumerRate_area/userList.length);
                            }else{
                                rate=0
                            }
                            let a=(rate.toFixed(2)-0)*100
                            dataCache[element]={
                                areaName:element,
                                newconsumerRate:a.toFixed(0)
                            }
                        }
                    }
                }else if (comment == '客户类型3') {
                    let typeList=await consumerRecoderModel.distinct("typeThree",{comment:1,"$and":[{typeThree:{$ne:null}},{typeThree:{$ne:''}}]})
                    let patt1 = new RegExp(/\s+/g);  
                    if(typeList.length>0){
                        for (let index1 = 0; index1 < typeList.length; index1++) {
                            let element = typeList[index1];
                            // let patt1 = new RegExp(/\s+/g);  
                            if (patt1.test(element)) {  
                                console.log("有空格");  
                                let d=element.replace(new RegExp(' ',"gm"), '')
                                if(!d){
                                    continue;
                                }
                            } 
                            if (!dataCache[element]) {
                                dataCache[element] = {
                                    areaName:element,
                                    newconsumerRate:''
                                }
                            }
                            userList = await consumerRecoderModel.distinct("userid", { typeThree: element, comment: 1,"$and":[{userid:{$in:userList2}},{userid:{$ne:null}}]});
                            let consumerRate_area=0;//客户拜访成功率
                            for (let userindex = 0; userindex < userList.length; userindex++) {
                                let element2 = userList[userindex];
                                let consumerRate_one_employee = await formDataService.getRate_NewConsumer_one_employeeDate1ToDate2(element2, date1, date2);
                                consumerRate_area+=consumerRate_one_employee;
                            }
                            let rate:any=0;
                            if(userList.length>0){
                                rate=(consumerRate_area/userList.length);
                            }else{
                                rate=0
                            }
                            let a=(rate.toFixed(2)-0)*100
                            dataCache[element]={
                                areaName:element,
                                newconsumerRate:a.toFixed(0)
                            }
                        }
                    }
                }else{
                    let sonData2:any={}
                    if(groupName.includes('区域')||groupName.includes('模块')){
                        sonData2=await formDataService.getsonDept(leaderDepts,false,access_token)
                    }
                    if (groupName.includes('区域')) {//区域负责人
                        if (comment== '省份') {//已经做好
                            sonDept=await formDataService.newArray(sonData2.listNames)
                            // console.log(sonDept,'sonDept_sonDept_sonDept_222222222222222222222222222222')
                            if(sonDept.length>0){
                                for (let index = 0; index < sonDept.length; index++) {
                                    let element = sonDept[index];
                                    if (!dataCache[element]) {
                                        dataCache[element] = {
                                            areaName:element,
                                            newconsumerRate:''
                                        }
                                    }
                                    userList = await visitMonthPlanModel.distinct("userId", { provinceName:element, comment: 1, deptId: { $in:son_list2 }, userId: { $ne: '' } }); 
                                    let consumerRate_area=0;//一个区域的客户拜访成功率
                                    for (let userindex = 0; userindex < userList.length; userindex++) {
                                        let element2 = userList[userindex];
                                        let consumerRate_one_employee = await formDataService.getRate_NewConsumer_one_employeeDate1ToDate2(element2, date1, date2);
                                        consumerRate_area+=consumerRate_one_employee;
                                    }
                                    let rate:any=0;
                                    if(userList.length>0){
                                        rate=(consumerRate_area/userList.length);
                                    }else{
                                        rate=0
                                    }
                                    let a=(rate.toFixed(2)-0)*100
                                    dataCache[element]={
                                        areaName:element,
                                        newconsumerRate:a.toFixed(0)
                                    }
                                }           
                            }   
                        }
                    }
                    if(groupName.includes('全国')||groupName.includes('模块')){//全国负责人或模块负责人
                        let sonDept:any=[]
                        let son_area_list:any[]=[]
                        let son_area_list2:any[]=[]
                        let son_province_list:any[]=[]
                        if(groupName.includes('模块')){
                            if(sonData2.list.length==0){
                                son_area_list=[];
                                son_province_list=[];
                            }else if(sonData.list.length==sonData2.list.length&&sonData2.list.length>0){
                                son_province_list=[];
                                son_area_list=sonData2.listNames
                                son_area_list2=sonData2.dept;
                            }else if(sonData.list.length !=sonData2.list.length&&sonData2.list.length>0){
                                son_area_list=sonData2.listNames;
                                for (let index2 = 0; index2 <sonData.listNames.length; index2++) {
                                    let element = sonData.listNames[index2];
                                    if(sonData2.listNames.includes(element)){
                                        son_province_list.push('')
                                    }else{
                                        son_province_list.push(element)
                                    }
                                }
                            }
                        }
                        if (comment== '区域') {//已经做好
                            let areaNameList:any[]=[]
                            if(groupName.includes('模块')){
                                areaNameList= son_area_list
                            }else if(groupName.includes('全国')){
                                areaNameList=await visitMonthPlanModel.distinct("areaName",{comment:1,"$and":[{areaName:{$ne:null}},{areaName:{$ne:''}}]})
                            }
                            if(areaNameList.length>0){
                                for (let index = 0; index < areaNameList.length; index++) {
                                    const element = areaNameList[index];
                                    if (!dataCache[element]) {
                                        dataCache[element] = {
                                            areaName:element,
                                            newconsumerRate:''
                                        }
                                    }
                                    userList = await visitMonthPlanModel.distinct("userId", {areaName:element, deptId: { $in: son_list2 },"$and":[{userId: { $ne: '' }},{userId: { $ne:null}}] });
                                    let consumerRate_area=0;//一个区域的客户拜访成功率
                                    for (let userindex = 0; userindex < userList.length; userindex++) {
                                        let element2 = userList[userindex];
                                        let consumerRate_one_employee = await formDataService.getRate_NewConsumer_one_employeeDate1ToDate2(element2, date1, date2);
                                        consumerRate_area+=consumerRate_one_employee;
                                    }
                                    let rate:any=0;
                                    if(userList.length>0){
                                        rate=(consumerRate_area/userList.length);
                                    }else{
                                        rate=0
                                    }
                                    let a=(rate.toFixed(2)-0)*100
                                    dataCache[element]={
                                        areaName:element,
                                        newconsumerRate:a.toFixed(0)
                                    }
                                }
                            }
                        }
                        if (comment == '省份') {//已经做完------未测试
                            let provinceNameList:any[]=[]
                            if(groupName.includes('模块')){
                                if(son_province_list.length>0){
                                    for (let proindex = 0; proindex < son_province_list.length; proindex++) {
                                        let element2 = son_province_list[proindex];
                                        if(element2){
                                            provinceNameList.push(element2)
                                        }
                                    }
                                }
                            }else if(groupName.includes('全国')){
                                provinceNameList=await visitMonthPlanModel.distinct("provinceName",{comment:1,"$and":[{provinceName:{$ne:null}},{provinceName:{$ne:''}}]})
                            }
                            if(provinceNameList.length>0){
                                for (let sonDeptindex = 0; sonDeptindex < provinceNameList.length; sonDeptindex++) {
                                    let pro = provinceNameList[sonDeptindex];
                                    if (!dataCache[pro]) {
                                        dataCache[pro] = {
                                            areaName:pro,
                                            newconsumerRate:''
                                        }
                                    }
                                    userList = await visitMonthPlanModel.distinct("userId", { provinceName:pro, comment: 1, deptId: { $in:son_list2 }, userId: { $ne: '' } }); 
                                    let consumerRate_area=0;//客户拜访成功率
                                    for (let userindex = 0; userindex < userList.length; userindex++) {
                                        let element2 = userList[userindex];
                                        let consumerRate_one_employee = await formDataService.getRate_NewConsumer_one_employeeDate1ToDate2(element2, date1, date2);
                                        consumerRate_area+=consumerRate_one_employee;
                                    }
                                    let rate:any=0;
                                    if(userList.length>0){
                                        rate=(consumerRate_area/userList.length);
                                    }else{
                                        rate=0
                                    }
                                    let a=(rate.toFixed(2)-0)*100
                                    dataCache[pro]={
                                        areaName:pro,
                                        newconsumerRate:a.toFixed(0)
                                    }
                                }
                            } 
                        }   
                    }
                }
            }
        }
        for (const key in dataCache) { //移植dataCache当中的属性到list当中
            if (dataCache.hasOwnProperty(key)) {
                const element = dataCache[key];
                list.push(element)
            }
        }
        if(list.length==0){
            list.push({
                areaName:'',
                newconsumerRate:0
            })
        }
        console.log(list,'list_list_list_list_list_list_list_list_新客户拜访达成率')
        return list;
    }

    //(三)已拜访门店拜访达标率------报表
    async getReach_VisitedStoreRate(startDate1: string, endDate1: string, character: string, comment: string, leaderId: string) {
        let dateData=await formDataService.getDate(startDate1,endDate1)
        let date1 = dateData.startDate;
        let date2= dateData.endDate;
        let month = moment(date1).format('MM');
        let num3 =parseInt( moment(date1).format('DD'));
        let num4 =parseInt( moment(date2).format('DD'));
        let dateList=await formDataService.getDate2(num3,num4,date1) //获取日期
        let access_token=await dTalkApiUtil.getAccessToken();
        let authority=await formDataService.getAuthrity(leaderId,access_token) //权限问题
        let groupName=authority.groupName;
        let leaderDepts = authority.leaderDepts
        let userId:any = '';
        let dataCache: any = []
        let list: any[] = [];
        let arr:any=[]
        
        let data5=await emplModel.find({})
        if(data5.length>0){
            for (let index5 = 0; index5 < data5.length; index5++) {
                let element = data5[index5];
                arr.push(element.userId)
            }
        }
        if (groupName.includes('销售')) {
            if (comment=='个人') {
                if(character==''){
                    userId = leaderId;
                }else{
                   userId=character;
                }
                let userInfo = await dTalkApi.getUserDetail(userId,access_token);
                let name:any='';
                if (userInfo&&userInfo.name) {
                    name=userInfo.name
                } else {
                    let data5=await visitMonthPlanModel.findOne({userId:userId,'$and':[{user:{$ne:null}},{user:{$ne:''}}]})
                    if(data5){
                        name=data5.user
                    }
                }
                if (dateList.length > 0) {
                    for (let index = 0; index < dateList.length; index++) {
                        let date = dateList[index];
                        let reachStoreRate_one_employee:any = await formDataService.getReachStoreRate_one_employeeDate1ToDate2(userId, date, date);
                       let a=(reachStoreRate_one_employee.toFixed(2)-0)*100
                        dataCache.push({
                            comment: '一天',
                            userId: userId,
                            userName: name,
                            date: date,
                            reachStoreRate: a.toFixed(0)
                        })
                    }
                }
            }
        }else{
            let userList: any[] = [];
            if(leaderDepts.length>0){
                let sonDept:any=[]
                let sonData=await formDataService.getsonDept(leaderDepts,true,access_token);
                let userList2=await formDataService.newArray(sonData.userIdData)
                let son_list2=sonData.list;
                if (comment == '个人') {
                    if(character !=''){
                       userId = character;
                        let userInfo = await dTalkApi.getUserDetail(userId,access_token);
                        let name:any='';
                        if (userInfo&&userInfo.name) {
                            name=userInfo.name
                        } else {
                            let data5=await visitMonthPlanModel.findOne({userId:userId,'$and':[{user:{$ne:null}},{user:{$ne:''}}]})
                            if(data5){
                                name=data5.user
                            }
                        }
                        if (dateList.length > 0) {
                            for (let index = 0; index < dateList.length; index++) {
                                let date = dateList[index];
                                let reachStoreRate_one_employee:any = await formDataService.getReachStoreRate_one_employeeDate1ToDate2(userId, date, date);
                                let a=(reachStoreRate_one_employee.toFixed(2)-0)*100
                                dataCache.push({
                                    comment: '一天',
                                    userId: userId,
                                    userName: name,
                                    date: date,
                                    reachStoreRate:a.toFixed(0)
                                })
                            }
                        }
                    }else{
                        if(userList2.length>0){
                            for (let index = 0; index < userList2.length; index++) {
                                let element2 = userList2[index];
                                if(!arr.includes(element2)){
                                    continue;
                                }
                                let emp=await visitMonthPlanModel.findOne({userId:element2,"$and":[{user:{$ne:''}},{user:{$ne:null}}]})
                                if (!dataCache[emp.user]) {
                                    dataCache[emp.user] = {
                                        areaName:emp.user,
                                        reachStoreRate:''
                                    }
                                }
                                let consumerRate_one_employee = await formDataService.getReachStoreRate_one_employeeDate1ToDate2(element2, date1, date2);
                                let rate:any=consumerRate_one_employee;
                                let a=(rate.toFixed(2)-0)*100
                                dataCache[emp.user]={
                                    areaName:emp.user,
                                    reachStoreRate:a.toFixed(0)
                                }
                            }
                        }
                    }
                }else if (comment == '店铺等级') {
                    let storeTypeList2=await storeInfoModel.distinct("store_classification",{"$and":[{store_classification:{$ne:null}},{store_classification:{$ne:''}}]});
                    //去除数组元素的首尾空格并去重
                    let newType:any=[]
                    for (let index = 0; index < storeTypeList2.length; index++) {
                        let element = storeTypeList2[index];
                        let a=element.trim();
                        newType.push(a)
                    }
                    let storeTypeList=await formDataService.newArray(newType)
                    if(storeTypeList.length>0){
                        for (let index = 0; index < storeTypeList.length; index++) {
                            let element = storeTypeList[index];
                            if (!dataCache[element]) {
                                dataCache[element] = {
                                    areaName:element,
                                    reachStoreRate:''
                                }
                            }
                            let consumerCodeList = await storeInfoModel.distinct("consumerCode", { store_classification: element,"$and":[{consumerCode:{$ne:null}},{consumerCode:{$ne:''}}]});
                            userList = await consumerRecoderModel.distinct("userid", { consumerCode: { $in: consumerCodeList }, comment: 1, "$and":[{userid:{$in:userList2}},{userid:{$ne:null}},{userid:{$ne:''}}] })
                            let reachStoreRate_area=0;//一个区域的客户拜访成功率
                            for (let userindex = 0; userindex < userList.length; userindex++) {
                                let element2 = userList[userindex];
                                let  reachStoreRate_one_employee = await formDataService.getReachStoreRate_one_employeeDate1ToDate2(element2, date1, date2);
                                reachStoreRate_area+=reachStoreRate_one_employee;
                            }
                            let rate:any=0;
                            if(userList.length>0){
                                rate=(reachStoreRate_area/userList.length);
                            }else{
                                rate=0
                            }
                            let a=(rate.toFixed(2)-0)*100
                            dataCache[element]={
                                areaName:element,
                                reachStoreRate: a.toFixed(0)
                            }
                        }
                    }
                }else{
                    let sonData2:any={}
                    if(groupName.includes('区域')||groupName.includes('模块')){
                        sonData2=await formDataService.getsonDept(leaderDepts,false,access_token)
                    }
                    if(groupName.includes('全国')||groupName.includes('模块')){
                        let sonDept:any=[]
                        let son_area_list:any[]=[]
                        let son_area_list2:any[]=[]
                        let son_province_list:any[]=[]
                        if(groupName.includes('模块')){
                            if(sonData2.list.length==0){
                                son_area_list=[];
                                son_province_list=[];
                            }else if(sonData.list.length==sonData2.list.length&&sonData2.list.length>0){
                                son_province_list=[];
                                son_area_list=sonData2.listNames
                                son_area_list2=sonData2.dept
                            }else if(sonData.list.length !=sonData2.list.length&&sonData2.list.length>0){
                                son_area_list=sonData2.listNames;
                                for (let index2 = 0; index2 <sonData.listNames.length; index2++) {
                                    let element = sonData.listNames[index2];
                                    if(sonData2.listNames.includes(element)){
                                        son_province_list.push('')
                                    }else{
                                        son_province_list.push(element)
                                    }
                                }
                            }
                        }
                        if (comment== '区域') {//已经做好
                            let areaNameList:any[]=[]
                            if(groupName.includes('模块')){
                                areaNameList= son_area_list
                            }else if(groupName.includes('全国')){
                                areaNameList=await visitMonthPlanModel.distinct("areaName",{comment:1,"$and":[{areaName:{$ne:null}},{areaName:{$ne:''}}]})
                            }
                            if(areaNameList.length>0){
                                for (let index = 0; index < areaNameList.length; index++) {
                                    const element = areaNameList[index];
                                    if (!dataCache[element]) {
                                        dataCache[element] = {
                                            areaName:element,
                                            reachStoreRate:''
                                        }
                                    }
                                    userList = await visitMonthPlanModel.distinct("userId", {areaName:element, deptId: { $in: son_list2 },"$and":[{userId: { $ne: '' }},{userId: { $ne:null}}] });
                                    let consumerRate_area=0;//一个区域的客户拜访成功率
                                    for (let userindex = 0; userindex < userList.length; userindex++) {
                                        let element2 = userList[userindex];
                                        let consumerRate_one_employee = await formDataService.getReachStoreRate_one_employeeDate1ToDate2(element2, date1, date2);
                                        consumerRate_area+=consumerRate_one_employee;
                                    }
                                    let rate:any=0;
                                    if(userList.length>0){
                                        rate=(consumerRate_area/userList.length);
                                    }else{
                                        rate=0
                                    }
                                    let a=(rate.toFixed(2)-0)*100
                                    dataCache[element]={
                                        areaName:element,
                                        reachStoreRate:a.toFixed(0)
                                    }
                                }
                            }
                        }
                        if (comment == '省份') {//已经做完------未测试
                            let provinceNameList:any[]=[]
                            if(groupName.includes('模块')){
                                if(son_province_list.length>0){
                                    for (let proindex = 0; proindex < son_province_list.length; proindex++) {
                                        let element2 = son_province_list[proindex];
                                        if(element2){
                                            provinceNameList.push(element2)
                                        }
                                    }
                                }
                            }else if(groupName.includes('全国')){
                                provinceNameList=await visitMonthPlanModel.distinct("provinceName",{comment:1,"$and":[{provinceName:{$ne:null}},{provinceName:{$ne:''}}]})
                            }
                            if(provinceNameList.length>0){
                                for (let sonDeptindex = 0; sonDeptindex < provinceNameList.length; sonDeptindex++) {
                                    let pro = provinceNameList[sonDeptindex];
                                    if (!dataCache[pro]) {
                                        dataCache[pro] = {
                                            areaName:pro,
                                            reachStoreRate:''
                                        }
                                    }
                                    userList = await visitMonthPlanModel.distinct("userId", { provinceName:pro, comment: 1, deptId: { $in:son_list2 }, userId: { $ne: '' } }); 
                                    let consumerRate_area=0;//一个区域的客户拜访成功率
                                    for (let userindex = 0; userindex < userList.length; userindex++) {
                                        let element2 = userList[userindex];
                                        let consumerRate_one_employee = await formDataService.getReachStoreRate_one_employeeDate1ToDate2(element2, date1, date2);
                                        consumerRate_area+=consumerRate_one_employee;
                                    }
                                    let rate:any=0;
                                    if(userList.length>0){
                                        rate=(consumerRate_area/userList.length);
                                    }else{
                                        rate=0
                                    }
                                    let a=(rate.toFixed(2)-0)*100
                                    dataCache[pro]={
                                        areaName:pro,
                                        reachStoreRate:a.toFixed(0)
                                    }
                                }
                            } 
                        } 
                    }
                    if (groupName.includes('区域')) {//区域负责人
                        if (comment == '省份') {//已经做完------未测试
                            sonDept=await formDataService.newArray(sonData2.listNames)
                            if(sonDept.length>0){
                                for (let sonDeptindex = 0; sonDeptindex < sonDept.length; sonDeptindex++) {
                                    let pro = sonDept[sonDeptindex];
                                    if (!dataCache[pro]) {
                                        dataCache[pro] = {
                                            areaName:pro,
                                            reachStoreRate:''
                                        }
                                    }
                                    userList = await visitMonthPlanModel.distinct("userId", { provinceName:pro, comment: 1, deptId: { $in:son_list2 }, userId: { $ne: '' } }); 
                                    // console.log('11111111111111111111',pro)
                                    // let sonData2 = await dTalkApi.getSondepIdList(pro.id, true);
                                    // let son_list3 = sonData2.department;
                                    // son_list3.push(parseInt(pro.id));//区域id列表
                                    // userList = await visitMonthPlanModel.distinct("userId", { provinceName:pro.name, comment: 1, deptId: { $in:son_list3 }, userId: { $ne: '' } }); 
                                    let consumerRate_area=0;//已拜访门店拜访达标率
                                    for (let userindex = 0; userindex < userList.length; userindex++) {
                                        let element2 = userList[userindex];
                                        let consumerRate_one_employee = await formDataService.getReachStoreRate_one_employeeDate1ToDate2(element2, date1, date2);
                                        consumerRate_area+=consumerRate_one_employee;
                                    }
                                    let rate:any=0;
                                    if(userList.length>0){
                                        rate=(consumerRate_area/userList.length);
                                    }else{
                                        rate=0
                                    }
                                    let a=(rate.toFixed(2)-0)*100
                                    dataCache[pro]={
                                        areaName:pro,
                                        reachStoreRate:a.toFixed(0)
                                    }
                                }
                            } 
                        }
                    }
                }   
            }
        }
        for (const key in dataCache) { //移植dataCache当中的属性到list当中
            if (dataCache.hasOwnProperty(key)) {
                const element = dataCache[key];
                list.push(element)
            }
        }
        if(list.length==0){
            list.push({
                areaName:'',
                reachStoreRate:0
            })
        }
        console.log(list,'list_list_list_list_list_list_list_list_已拜访门店拜访达标率')
        return list;
    }

    //(四)门店拜访进度------报表-----------------------暂时无问题
    async getVisitStoreSchadule(startDate1: string, endDate1: string, character: string, comment: string, leaderId: string) {
        let list: any[] = [];
        let dateData=await formDataService.getDate(startDate1,endDate1)
        let date1 = dateData.startDate;
        let date2= dateData.endDate;
        let month = moment(date1).format('MM');
        let num3 =parseInt( moment(date1).format('DD'));
        let num4 =parseInt( moment(date2).format('DD'));
        let dateList=await formDataService.getDate2(num3,num4,date1)  //获取日期
        let access_token=await dTalkApiUtil.getAccessToken();
        let authority=await formDataService.getAuthrity(leaderId,access_token)  //权限问题
        let groupName=authority.groupName;
        let leaderDepts = authority.leaderDepts
        let userId:any = '';
        let dataCache: any = []
        let arr:any=[]
        
        let data5=await emplModel.find({})
        if(data5.length>0){
            for (let index5 = 0; index5 < data5.length; index5++) {
                let element = data5[index5];
                arr.push(element.userId)
            }
        }
        if (groupName.includes('销售')) {
            if (comment=='个人') {
                if(character==''){
                    userId = leaderId;
                }else{
                   userId=character;
                }
                let userInfo = await dTalkApi.getUserDetail(userId,access_token);
                let name:any='';
                if (userInfo&&userInfo.name) {
                    name=userInfo.name
                } else {
                    let data5=await visitMonthPlanModel.findOne({userId:userId,'$and':[{user:{$ne:null}},{user:{$ne:''}}]})
                    if(data5){
                        name=data5.user
                    }
                }
                if (dateList.length > 0) {
                    for (let index = 0; index < dateList.length; index++) {
                        let date = dateList[index];
                        let shop_schedule_one_employee:any = await formDataService.getShop_schedule_one_employeeDate1ToDate2(userId, date, date);
                        let num:any=(shop_schedule_one_employee.toFixed(2)-0)*100
                        dataCache.push({
                            comment: '一天',
                            userId: userId,
                            userName: name,
                            date: date,
                            storeSchadule: num.toFixed(0)
                        })
                    }
                }
            }
        }else{
            let userList: any[] = [];
            if(leaderDepts.length>0){
                let sonDept:any=[]
                let sonData=await formDataService.getsonDept(leaderDepts,true,access_token);
                let userList2=await formDataService.newArray(sonData.userIdData)
                let son_list2=sonData.list;
                if (comment == '个人') {//已经做好
                    if(character !=''){
                       userId = character;
                        let userInfo = await dTalkApi.getUserDetail(userId,access_token);
                        let name:any='';
                        if (userInfo&&userInfo.name) {
                            name=userInfo.name
                        } else {
                            let data5=await visitMonthPlanModel.findOne({userId:userId,'$and':[{user:{$ne:null}},{user:{$ne:''}}]})
                            if(data5){
                                name=data5.user
                            }
                        }
                        if (dateList.length > 0) {
                            for (let index = 0; index < dateList.length; index++) {
                                let date = dateList[index];
                                let shop_schedule_one_employee:any = await formDataService.getShop_schedule_one_employeeDate1ToDate2(userId, date, date);
                                let num:any=(shop_schedule_one_employee.toFixed(2)-0)*100
                                dataCache.push({
                                    comment: '一天',
                                    userId: userId,
                                    userName: name,
                                    date: date,
                                    storeSchadule: num.toFixed(0)
                                })
                            }
                        }
                    }else{
                        if(userList2.length>0){
                            for (let index = 0; index < userList2.length; index++) {
                                let element2 = userList2[index];
                                if(!arr.includes(element2)){
                                    continue;
                                }
                                let emp=await visitMonthPlanModel.findOne({userId:element2,"$and":[{user:{$ne:''}},{user:{$ne:null}}]})
                                if (!dataCache[emp.user]) {
                                    dataCache[emp.user] = {
                                        areaName:emp.user,
                                        storeSchadule:''
                                    }
                                }
                                let consumerRate_one_employee = await formDataService.getShop_schedule_one_employeeDate1ToDate2(element2, date1, date2);
                                let rate:any=consumerRate_one_employee
                                let a=(rate.toFixed(2)-0)*100
                                dataCache[emp.user]={
                                    areaName:emp.user,
                                    storeSchadule:a.toFixed(0)
                                }
                            }
                        }
                    }
                }else if (comment == '店铺等级') {
                    let storeTypeList2=await storeInfoModel.distinct("store_classification",{"$and":[{store_classification:{$ne:null}},{store_classification:{$ne:''}}]});
                    let newType:any=[]
                    for (let index = 0; index < storeTypeList2.length; index++) { //去除数组元素的首尾空格
                        let element = storeTypeList2[index];
                        let a=element.trim();
                        newType.push(a)
                    }
                    let storeTypeList=await formDataService.newArray(newType)
                    if(storeTypeList.length>0){
                        for (let index = 0; index < storeTypeList.length; index++) {
                            let element = storeTypeList[index];
                            if (!dataCache[element]) {
                                dataCache[element] = {
                                    areaName:element,
                                    storeSchadule:''
                                }
                            }
                            let consumerCodeList = await storeInfoModel.distinct("consumerCode", { store_classification: element,"$and":[{consumerCode:{$ne:null}},{consumerCode:{$ne:''}}]  });
                            userList = await consumerRecoderModel.distinct("userid", { consumerCode: { $in: consumerCodeList }, comment: 1, "$and":[{userid:{$in:userList2}},{userid:{$ne:null}},{userid:{$ne:''}}] })
                            let reachStoreRate_area=0;//一个区域的客户拜访成功率
                            for (let userindex = 0; userindex < userList.length; userindex++) {
                                let element2 = userList[userindex];
                                let  reachStoreRate_one_employee = await formDataService.getShop_schedule_one_employeeDate1ToDate2(element2, date1, date2);
                                reachStoreRate_area+=reachStoreRate_one_employee;
                            }
                            let rate:any=0;
                            if(userList.length>0){
                                rate=(reachStoreRate_area/userList.length);
                            }else{
                                rate=0
                            }
                            let a= (rate.toFixed(2)-0)*100
                            dataCache[element]={
                                areaName:element,
                                storeSchadule: a.toFixed(0)
                            } 
                        }
                    }
                }else if (comment == '城市类型') {
                    let cityTypeList=await storeInfoModel.distinct("cityType",{  "$and":[{cityType:{$ne:null}},{cityType:{$ne:''}}]});
                    if(cityTypeList.length>0){
                        for (let cityindex = 0; cityindex < cityTypeList.length; cityindex++) {
                            let element = cityTypeList[cityindex];
                            if (!dataCache[element]) {
                                dataCache[element] = {
                                    areaName:element,
                                    storeSchadule:''
                                }
                            } 
                            let consumerCodeList = await storeInfoModel.distinct("consumerCode", { cityType: element,  "$and":[{consumerCode:{$ne:null}},{consumerCode:{$ne:''}}]  });
                            userList = await consumerRecoderModel.distinct("userid", { consumerCode: { $in: consumerCodeList }, comment: 1, "$and":[{userid:{$in:userList2}},{userid:{$ne:null}},{userid:{$ne:''}}] })
                            let storeSchadule_total=0;//门店拜访进度
                            for (let userindex = 0; userindex < userList.length; userindex++) {
                                let element2 = userList[userindex];
                               let shop_schedule_one_employee = await formDataService.getShop_schedule_one_employeeDate1ToDate2(element2, date1, date2);
                               storeSchadule_total += shop_schedule_one_employee;
                            }
                            let rate:any=0;
                            if(userList.length>0){
                                rate=(storeSchadule_total/userList.length);
                            }else{
                                rate=0
                            }
                            let a= (rate.toFixed(2)-0)*100
                            dataCache[element]={
                                areaName:element,
                                storeSchadule: a.toFixed(0)
                            } 
                        }
                    } 
                }else{
                    let sonData2:any={}
                    if(groupName.includes('区域')||groupName.includes('模块')){
                        sonData2=await formDataService.getsonDept(leaderDepts,false,access_token)
                    }
                    if (groupName.includes('区域')) {//区域负责人
                        if (comment == '省份') {//已经做完------未测试
                            sonDept=await formDataService.newArray(sonData2.listNames)
                            if(sonDept.length>0){
                                for (let sonDeptindex = 0; sonDeptindex < sonDept.length; sonDeptindex++) {
                                    let pro = sonDept[sonDeptindex];
                                    if (!dataCache[pro]) {
                                        dataCache[pro] = {
                                            areaName:pro,
                                            storeSchadule:''
                                        }
                                    }
                                    userList = await visitMonthPlanModel.distinct("userId", { provinceName:pro, comment: 1, deptId: { $in:son_list2 }, userId: { $ne: '' } }); 
                                    let consumerRate_area=0;//门店拜访进度
                                    for (let userindex = 0; userindex < userList.length; userindex++) {
                                        let element2 = userList[userindex];
                                        let consumerRate_one_employee = await formDataService.getShop_schedule_one_employeeDate1ToDate2(element2, date1, date2);
                                        consumerRate_area+=consumerRate_one_employee;
                                    }
                                    let rate:any=0;
                                    if(userList.length>0){
                                        rate=(consumerRate_area/userList.length);
                                    }else{
                                        rate=0
                                    }
                                    let a=(rate.toFixed(2)-0)*100
                                    dataCache[pro]={
                                        areaName:pro,
                                        storeSchadule:a.toFixed(0)
                                    }
                                }
                            } 
                        }
                    }
                    if(groupName.includes('全国')||groupName.includes('模块')){
                        let sonDept:any=[]
                        let son_area_list:any[]=[]
                        let son_area_list2:any[]=[]
                        let son_province_list:any[]=[]
                        if(groupName.includes('模块')){
                            if(sonData2.list.length==0){
                                son_area_list=[];
                                son_province_list=[];
                            }else if(sonData.list.length==sonData2.list.length&&sonData2.list.length>0){
                                son_province_list=[];
                                son_area_list=sonData2.listNames
                                son_area_list2=sonData2.dept
                            }else if(sonData.list.length !=sonData2.list.length&&sonData2.list.length>0){
                                son_area_list=sonData2.listNames;
                                for (let index2 = 0; index2 <sonData.listNames.length; index2++) {
                                    let element = sonData.listNames[index2];
                                    if(sonData2.listNames.includes(element)){
                                        son_province_list.push('')
                                    }else{
                                        son_province_list.push(element)
                                    }
                                }
                            }
                        }
                        if (comment== '区域') {//已经做好
                            let areaNameList:any[]=[]
                            if(groupName.includes('模块')){
                                areaNameList= son_area_list
                            }else if(groupName.includes('全国')){
                                areaNameList=await visitMonthPlanModel.distinct("areaName",{comment:1,"$and":[{areaName:{$ne:null}},{areaName:{$ne:''}}]})
                            }
                            if(areaNameList.length>0){
                                for (let index = 0; index < areaNameList.length; index++) {
                                    const element = areaNameList[index];
                                    if (!dataCache[element]) {
                                        dataCache[element] = {
                                            areaName:element,
                                            storeSchadule:''
                                        }
                                    }
                                    userList = await visitMonthPlanModel.distinct("userId", {areaName:element, deptId: { $in: son_list2 },"$and":[{userId: { $ne: '' }},{userId: { $ne:null}}] });
                                    let storeSchadule=0;//门店拜访进度
                                    for (let userindex = 0; userindex < userList.length; userindex++) {
                                        let element2 = userList[userindex];
                                        let consumerRate_one_employee = await formDataService.getShop_schedule_one_employeeDate1ToDate2(element2, date1, date2);
                                        storeSchadule+=consumerRate_one_employee;
                                    }
                                    let rate:any=0;
                                    if(userList.length>0){
                                        rate=(storeSchadule/userList.length);
                                    }else{
                                        rate=0
                                    }
                                    let a=(rate.toFixed(2)-0)*100
                                    dataCache[element]={
                                        areaName:element,
                                        storeSchadule:a.toFixed(0)
                                    }
                                }
                            }
                        }
                        if (comment == '省份') {//已经做完------未测试
                            let provinceNameList:any[]=[]
                            if(groupName.includes('模块')){
                                if(son_province_list.length>0){
                                    for (let proindex = 0; proindex < son_province_list.length; proindex++) {
                                        let element2 = son_province_list[proindex];
                                        if(element2){
                                            provinceNameList.push(element2)
                                        }
                                    }
                                }
                            }else if(groupName.includes('全国')){
                                provinceNameList=await visitMonthPlanModel.distinct("provinceName",{comment:1,"$and":[{provinceName:{$ne:null}},{provinceName:{$ne:''}}]})
                            }
                            if(provinceNameList.length>0){
                                for (let sonDeptindex = 0; sonDeptindex < provinceNameList.length; sonDeptindex++) {
                                    let pro = provinceNameList[sonDeptindex];
                                    if (!dataCache[pro]) {
                                        dataCache[pro] = {
                                            areaName:pro,
                                            storeSchadule:''
                                        }
                                    }
                                    userList = await visitMonthPlanModel.distinct("userId", { provinceName:pro, comment: 1, deptId: { $in:son_list2 }, userId: { $ne: '' } }); 
                                    let consumerRate_area=0;//一个区域的客户拜访成功率
                                    for (let userindex = 0; userindex < userList.length; userindex++) {
                                        let element2 = userList[userindex];
                                        let consumerRate_one_employee = await formDataService.getShop_schedule_one_employeeDate1ToDate2(element2, date1, date2);
                                        consumerRate_area+=consumerRate_one_employee;
                                    }
                                    let rate:any=0;
                                    if(userList.length>0){
                                        rate=(consumerRate_area/userList.length);
                                    }else{
                                        rate=0
                                    }
                                    let a=(rate.toFixed(2)-0)*100
                                    dataCache[pro]={
                                        areaName:pro,
                                        storeSchadule:a.toFixed(0)
                                    }
                                }
                            } 
                        }  
                    }
                }
            }
        } 
        for (const key in dataCache) { //移植dataCache当中的属性到list当中
            if (dataCache.hasOwnProperty(key)) {
                const element = dataCache[key];
                list.push(element)
            }
        }
        if(list.length==0){
            list.push({
                areaName:'',
                storeSchadule:0
            })
        }
        console.log(list,'list_list_list_list_list_list_list_list_门店拜访进度')
        return list;
    }
    
    //(五)签到率------报表
    async getCheckInRate(startDate1: string, endDate1: string, character: string, comment: string, leaderId) {
        let list: any[] = [];
        let dateData=await formDataService.getDate(startDate1,endDate1)
        let date1 = dateData.startDate;
        let date2= dateData.endDate;
        let month = moment(date1).format('MM');
        let num3 =parseInt( moment(date1).format('DD'));
        let num4 =parseInt( moment(date2).format('DD'));
        let dateList=await formDataService.getDate2(num3,num4,date1) //获取日期
        let access_token=await dTalkApiUtil.getAccessToken();
        let authority=await formDataService.getAuthrity(leaderId,access_token) //权限问题
        let groupName=authority.groupName;
        let leaderDepts = authority.leaderDepts
        let userId:any = '';
        let dataCache: any = []
        let arr:any=[]
       
        let data5=await emplModel.find({})
        if(data5.length>0){
            for (let index5 = 0; index5 < data5.length; index5++) {
                let element = data5[index5];
                arr.push(element.userId)
            }
        }
        if (groupName.includes('销售')) {
            if (comment=='个人') {
                if(character==''){
                    userId = leaderId;
                }else{
                   userId=character;
                }
                let userInfo = await dTalkApi.getUserDetail(userId,access_token);
                let name:any='';
                if (userInfo&&userInfo.name) {
                    name=userInfo.name
                } else {
                    let data5=await visitMonthPlanModel.findOne({userId:userId,'$and':[{user:{$ne:null}},{user:{$ne:''}}]})
                    if(data5){
                        name=data5.user
                    }
                }
                if (dateList.length > 0) {
                    for (let index = 0; index < dateList.length; index++) {
                        let date = dateList[index];
                        let checkInRate_oneemployee:any = await formDataService.getCheckInRate_one_employeeDate1ToDate2(userId, date, date);
                        let num:any=(checkInRate_oneemployee.toFixed(2)-0)*100
                        dataCache.push({
                            comment: '一天',
                            userId: userId,
                            userName:name,
                            date: date,
                            checkInRate:num.toFixed(0)
                        })
                    }
                }
            }
        }else{
            let userList: any[] = [];
            if(leaderDepts.length>0){
                let sonDept:any=[]
                let sonData=await formDataService.getsonDept(leaderDepts,true,access_token);
                let userList2=await formDataService.newArray(sonData.userIdData)
                let son_list2=sonData.list;
                if (comment == '个人') {
                    if(character !=''){
                        userId = character;
                        let userInfo = await dTalkApi.getUserDetail(userId,access_token);
                        let name:any='';
                        if (userInfo&&userInfo.name) {
                            name=userInfo.name
                        } else {
                            let data5=await visitMonthPlanModel.findOne({userId:userId,'$and':[{user:{$ne:null}},{user:{$ne:''}}]})
                            if(data5){
                                name=data5.user
                            }
                        }
                        if (dateList.length > 0) {
                            for (let index = 0; index < dateList.length; index++) {
                                let date = dateList[index];
                                let checkInRate_oneemployee:any = await formDataService.getCheckInRate_one_employeeDate1ToDate2(userId, date, date);
                                let num:any=(checkInRate_oneemployee.toFixed(2)-0)*100
                                dataCache.push({
                                    comment: '一天',
                                    userId: userId,
                                    userName: name,
                                    date: date,
                                    checkInRate:num.toFixed(0)
                                })
                            }
                        }
                    }else{
                        if(userList2.length>0){
                            for (let index = 0; index < userList2.length; index++) {
                                let element2 = userList2[index];
                                if(!arr.includes(element2)){
                                    continue;
                                }
                                let emp=await visitMonthPlanModel.findOne({userId:element2,"$and":[{user:{$ne:''}},{user:{$ne:null}}]})
                                if (!dataCache[emp.user]) {
                                    dataCache[emp.user] = {
                                        areaName:emp.user,
                                        checkInRate:''
                                    }
                                }
                                let consumerRate_one_employee = await formDataService.getCheckInRate_one_employeeDate1ToDate2(element2, date1, date2);
                                let rate:any=consumerRate_one_employee
                                let a=(rate.toFixed(2)-0)*100
                                dataCache[emp.user]={
                                    areaName:emp.user,
                                    checkInRate:a.toFixed(0)
                                }
                            }
                        }
                    }
                }else{
                    let sonData2:any={}
                    if(groupName.includes('区域')||groupName.includes('模块')){
                        sonData2=await formDataService.getsonDept(leaderDepts,false,access_token)
                    }
                    if (groupName.includes('区域')) {//区域负责人
                        if (comment == '省份') {//已经做完------未测试
                            sonDept=await formDataService.newArray(sonData2.listNames)
                            if(sonDept.length>0){
                                for (let sonDeptindex = 0; sonDeptindex < sonDept.length; sonDeptindex++) {
                                    let pro = sonDept[sonDeptindex];
                                    if (!dataCache[pro]) {
                                        dataCache[pro] = {
                                            areaName:pro,
                                            checkInRate:''
                                        }
                                    }
                                    userList = await visitMonthPlanModel.distinct("userId", { provinceName:pro, comment: 1, deptId: { $in:son_list2 }, userId: { $ne: '' } }); 
                                    let consumerRate_area=0;//一个区域的客户拜访成功率
                                    for (let userindex = 0; userindex < userList.length; userindex++) {
                                        let element2 = userList[userindex];
                                        let consumerRate_one_employee = await formDataService.getCheckInRate_one_employeeDate1ToDate2(element2, date1, date2);
                                        consumerRate_area+=consumerRate_one_employee;
                                    }
                                    let rate:any=0;
                                    if(userList.length>0){
                                        rate=(consumerRate_area/userList.length);
                                    }else{
                                        rate=0
                                    }
                                    let a=(rate.toFixed(2)-0)*100
                                    dataCache[pro]={
                                        areaName:pro,
                                        checkInRate:a.toFixed(0)
                                    }
                                }
                            } 
                        }
                    }
                    if(groupName.includes('全国')||groupName.includes('模块')){
                        let sonDept:any=[]
                        let son_area_list:any[]=[]
                        let son_area_list2:any[]=[]
                        let son_province_list:any[]=[]
                        if(groupName.includes('模块')){
                            if(sonData2.list.length==0){
                                son_area_list=[];
                                son_province_list=[];
                            }else if(sonData.list.length==sonData2.list.length&&sonData2.list.length>0){
                                son_province_list=[];
                                son_area_list=sonData2.listNames
                                son_area_list2=sonData2.dept
                            }else if(sonData.list.length !=sonData2.list.length&&sonData2.list.length>0){
                                son_area_list=sonData2.listNames;
                                for (let index2 = 0; index2 <sonData.listNames.length; index2++) {
                                    let element = sonData.listNames[index2];
                                    if(sonData2.listNames.includes(element)){
                                        son_province_list.push('')
                                    }else{
                                        son_province_list.push(element)
                                    }
                                }
                            }
                        }
                        if (comment== '区域') {//已经做好
                            let areaNameList:any[]=[]
                            if(groupName.includes('模块')){
                                areaNameList= son_area_list
                            }else if(groupName.includes('全国')){
                                areaNameList=await visitMonthPlanModel.distinct("areaName",{comment:1,"$and":[{areaName:{$ne:null}},{areaName:{$ne:''}}]})
                            }
                            if(areaNameList.length>0){
                                for (let index = 0; index < areaNameList.length; index++) {
                                    const element = areaNameList[index];
                                    if (!dataCache[element]) {
                                        dataCache[element] = {
                                            areaName:element,
                                            checkInRate:''
                                        }
                                    }
                                    userList = await visitMonthPlanModel.distinct("userId", {areaName:element, deptId: { $in: son_list2 },"$and":[{userId: { $ne: '' }},{userId: { $ne:null}}] });
                                    let consumerRate_area=0;//一个区域的签到率
                                    for (let userindex = 0; userindex < userList.length; userindex++) {
                                        let element2 = userList[userindex];
                                        let consumerRate_one_employee = await formDataService.getCheckInRate_one_employeeDate1ToDate2(element2, date1, date2);
                                        consumerRate_area+=consumerRate_one_employee;
                                    }
                                    let rate:any=0;
                                    if(userList.length>0){
                                        rate=(consumerRate_area/userList.length);
                                    }else{
                                        rate=0
                                    }
                                    let a=(rate.toFixed(2)-0)*100
                                    dataCache[element]={
                                        areaName:element,
                                        checkInRate:a.toFixed(0)
                                    }
                                }
                            }
                        }
                        if (comment == '省份') {//已经做完------未测试
                            let provinceNameList:any[]=[]
                            if(groupName.includes('模块')){
                                if(son_province_list.length>0){
                                    for (let proindex = 0; proindex < son_province_list.length; proindex++) {
                                        let element2 = son_province_list[proindex];
                                        if(element2){
                                            provinceNameList.push(element2)
                                        }
                                    }
                                }
                            }else if(groupName.includes('全国')){
                                provinceNameList=await visitMonthPlanModel.distinct("provinceName",{comment:1,"$and":[{provinceName:{$ne:null}},{provinceName:{$ne:''}}]})
                            }
                            if(provinceNameList.length>0){
                                for (let sonDeptindex = 0; sonDeptindex < provinceNameList.length; sonDeptindex++) {
                                    let pro = provinceNameList[sonDeptindex];
                                    if (!dataCache[pro]) {
                                        dataCache[pro] = {
                                            areaName:pro,
                                            checkInRate:''
                                        }
                                    }
                                    // let provinceId=await visitMonthPlanModel.findOne({provinceName:pro,"$and":[{province:{$ne:null}},{province:{$ne:''}}]})
                                    userList = await visitMonthPlanModel.distinct("userId", { provinceName:pro, comment: 1, deptId: { $in:son_list2 }, userId: { $ne: '' } }); 
                                    let consumerRate_area=0;//一个省份的签到率
                                    for (let userindex = 0; userindex < userList.length; userindex++) {
                                        let element2 = userList[userindex];
                                        let consumerRate_one_employee = await formDataService.getCheckInRate_one_employeeDate1ToDate2(element2, date1, date2);
                                        consumerRate_area+=consumerRate_one_employee;
                                    }
                                    let rate:any=0;
                                    if(userList.length>0){
                                        rate=(consumerRate_area/userList.length);
                                    }else{
                                        rate=0
                                    }
                                    let a=(rate.toFixed(2)-0)*100
                                    dataCache[pro]={
                                        areaName:pro,
                                        checkInRate:a.toFixed(0)
                                    }
                                }
                            }     
                        }
                    }
                }
            }
        } 
        for (const key in dataCache) { //移植dataCache当中的属性到list当中
            if (dataCache.hasOwnProperty(key)) {
                const element = dataCache[key];
                list.push(element)
            }
        }
        if(list.length==0){
            list.push({
                areaName:'',
                checkInRate:0
            })
        }
        console.log(list,'list_list_list_list_list_list_list_list_签到率')
        return list;
    }

    //(六)出差频率------报表
    async getTravelFrequency(startDate1: string, endDate1: string, character: string, comment: string, leaderId: string) {
        let list: any[] = [];
        let dateData=await formDataService.getDate(startDate1,endDate1)
        let date1 = dateData.startDate;
        let date2= dateData.endDate;
        let month = moment(date1).format('MM');
        let num3 =parseInt( moment(date1).format('DD'));
        let num4 =parseInt( moment(date2).format('DD'));
        let dateList=await formDataService.getDate2(num3,num4,date1) //获取日期
        let access_token=await dTalkApiUtil.getAccessToken();
        let authority=await formDataService.getAuthrity(leaderId,access_token) //权限问题
        let groupName=authority.groupName;
        let leaderDepts = authority.leaderDepts
        let userId:any = '';
        let dataCache: any = []
        let arr:any=[]
       
        let data5=await emplModel.find({})
        if(data5.length>0){
            for (let index5 = 0; index5 < data5.length; index5++) {
                let element = data5[index5];
                arr.push(element.userId)
            }
        }
        if (groupName.includes('销售')) {
            if (comment=='个人') {
                if(character==''){
                    userId = leaderId;
                }else{
                   userId=character;
                }
                let userInfo = await dTalkApi.getUserDetail(userId,access_token);
                let name:any='';
                if (userInfo&&userInfo.name) {
                    name=userInfo.name
                } else {
                    let data5=await visitMonthPlanModel.findOne({userId:userId,'$and':[{user:{$ne:null}},{user:{$ne:''}}]})
                    if(data5){
                        name=data5.user
                    }
                }
                if (dateList.length > 0) {
                    for (let index = 0; index < dateList.length; index++) {
                        let date = dateList[index];
                        let travelFrequency_oneEmployee = await formDataService.getgetTravelFrequency_oneemployeeDate1ToDate2(userId, date, date);
                        let num:any=(travelFrequency_oneEmployee.toFixed(2)-0)*100
                        dataCache.push({
                            comment: '一天',
                            userId: userId,
                            userName: name,
                            date: date,
                            travelFrequency:num.toFixed(0)
                        })
                    }
                }
            }
        }else{
            let userList: any[] = [];
            if(leaderDepts.length>0){
                let sonDept:any=[]
                let sonData=await formDataService.getsonDept(leaderDepts,true,access_token);
                let userList2=await formDataService.newArray(sonData.userIdData)
                let son_list2=sonData.list;
                if (comment == '个人') {
                    if(character !=''){
                        userId = character;
                        let userInfo = await dTalkApi.getUserDetail(userId,access_token);
                        let name:any='';
                        if (userInfo&&userInfo.name) {
                            name=userInfo.name
                        } else {
                            let data5=await visitMonthPlanModel.findOne({userId:userId,'$and':[{user:{$ne:null}},{user:{$ne:''}}]})
                            if(data5){
                                name=data5.user
                            }
                        }
                        if (dateList.length > 0) {
                            for (let index = 0; index < dateList.length; index++) {
                                let date = dateList[index];
                                let travelFrequency_oneEmployee = await formDataService.getgetTravelFrequency_oneemployeeDate1ToDate2(userId, date, date);
                                let num:any=(travelFrequency_oneEmployee.toFixed(2)-0)*100
                                dataCache.push({
                                    comment: '一天',
                                    userId: userId,
                                    userName:name,
                                    date: date,
                                    travelFrequency:num.toFixed(0)
                                })
                            }
                        }
                    }else{
                        if(userList2.length>0){
                            for (let index = 0; index < userList2.length; index++) {
                                let element2 = userList2[index];
                                if(!arr.includes(element2)){
                                    continue;
                                }
                                let emp=await visitMonthPlanModel.findOne({userId:element2,"$and":[{user:{$ne:''}},{user:{$ne:null}}]})
                                if (!dataCache[emp.user]) {
                                    dataCache[emp.user] = {
                                        areaName:emp.user,
                                        travelFrequency:''
                                    }
                                }
                                let consumerRate_one_employee = await formDataService.getgetTravelFrequency_oneemployeeDate1ToDate2(element2, date1, date2);
                                let rate:any=consumerRate_one_employee
                                let a=(rate.toFixed(2)-0)*100
                                dataCache[emp.user]={
                                    areaName:emp.user,
                                    travelFrequency:a.toFixed(0)
                                }
                            }
                        }
                    }
                }else if (comment == '城市类型') {
                    let cityTypeList=await storeInfoModel.distinct("cityType",{"$and":[{cityType:{$ne:''}},{cityType:{$ne:null}}]});
                    if(cityTypeList.length>0){
                        for (let cityindex = 0; cityindex < cityTypeList.length; cityindex++) {
                            let element = cityTypeList[cityindex];
                            if (!dataCache[element]) {
                                dataCache[element] = {
                                    areaName:element,
                                    travelFrequency:''
                                }
                            }
                            let consumerCodeList = await storeInfoModel.distinct("consumerCode", { cityType: element, consumerCode: { $ne:null} });
                            userList = await consumerRecoderModel.distinct("userid", { consumerCode: { $in: consumerCodeList }, comment: 1, "$and":[{userid:{$in:userList2}},{userid:{$ne:''}},{userid:{$ne:null}}] })
                            let travelFrequency_total=0;//出差频率
                            for (let userindex = 0; userindex < userList.length; userindex++) {
                                let element2 = userList[userindex];
                                let travelFrequency_oneEmployee = await formDataService.getgetTravelFrequency_oneemployeeDate1ToDate2(element2, date1, date2);
                                travelFrequency_total += travelFrequency_oneEmployee;
                            }
                            let rate:any=0;
                            if(userList.length>0){
                                rate=(travelFrequency_total/userList.length);
                            }else{
                                rate=0
                            }
                            let a= (rate.toFixed(2)-0)*100
                            dataCache[element]={
                                areaName:element,
                                travelFrequency:a.toFixed(0)
                            }
                        }
                    }
                }else{
                    let sonData2:any={}
                    if(groupName.includes('区域')||groupName.includes('模块')){
                        sonData2=await formDataService.getsonDept(leaderDepts,false,access_token)
                    }
                    if (groupName.includes('区域')) {//区域负责人
                        if (comment == '省份') {//已经做完------未测试
                            sonDept=await formDataService.newArray(sonData2.listNames)
                            if(sonDept.length>0){
                                for (let sonDeptindex = 0; sonDeptindex < sonDept.length; sonDeptindex++) {
                                    let pro = sonDept[sonDeptindex];
                                    if (!dataCache[pro]) {
                                        dataCache[pro] = {
                                            areaName:pro,
                                            travelFrequency:''
                                        }
                                    }
                                    userList = await visitMonthPlanModel.distinct("userId", { provinceName:pro, deptId: { $in:son_list2 }, userId: { $ne: '' } }); 
                                    let consumerRate_area=0;//出差频率
                                    for (let userindex = 0; userindex < userList.length; userindex++) {
                                        let element2 = userList[userindex];
                                        let consumerRate_one_employee = await formDataService.getgetTravelFrequency_oneemployeeDate1ToDate2(element2, date1, date2);
                                        consumerRate_area+=consumerRate_one_employee;
                                    }
                                    let rate:any=0;
                                    if(userList.length>0){
                                        rate=(consumerRate_area/userList.length);
                                    }else{
                                        rate=0
                                    }
                                    let a=(rate.toFixed(2)-0)*100
                                    dataCache[pro]={
                                        areaName:pro,
                                        travelFrequency:a.toFixed(0)
                                    }
                                }
                            } 
                        }
                    }
                    if(groupName.includes('全国')||groupName.includes('模块')){
                        let son_area_list:any[]=[]
                        let son_area_list2:any[]=[]
                        let son_province_list:any[]=[]
                        if(groupName.includes('模块')){
                            if(sonData2.list.length==0){
                                son_area_list=[];
                                son_province_list=[];
                            }else if(sonData.list.length==sonData2.list.length&&sonData2.list.length>0){
                                son_province_list=[];
                                son_area_list=sonData2.listNames
                                son_area_list2=sonData2.dept
                            }else if(sonData.list.length !=sonData2.list.length&&sonData2.list.length>0){
                                son_area_list=sonData2.listNames;
                                for (let index2 = 0; index2 <sonData.listNames.length; index2++) {
                                    let element = sonData.listNames[index2];
                                    if(sonData2.listNames.includes(element)){
                                        son_province_list.push('')
                                    }else{
                                        son_province_list.push(element)
                                    }
                                }
                            }
                        }
                        if (comment== '区域') {
                            let areaNameList:any[]=[]
                            if(groupName.includes('模块')){
                                areaNameList= son_area_list
                            }else if(groupName.includes('全国')){
                                areaNameList=await visitMonthPlanModel.distinct("areaName",{comment:1,"$and":[{areaName:{$ne:null}},{areaName:{$ne:''}}]})
                            }
                            if(areaNameList.length>0){
                                for (let index = 0; index < areaNameList.length; index++) {
                                    const element = areaNameList[index];
                                    if (!dataCache[element]) {
                                        dataCache[element] = {
                                            areaName:element,
                                            travelFrequency:''
                                        }
                                    }
                                    userList = await visitMonthPlanModel.distinct("userId", {areaName:element, deptId: { $in: son_list2 },"$and":[{userId: { $ne: '' }},{userId: { $ne:null}}] });
                                    let consumerRate_area=0;//出差频率
                                    for (let userindex = 0; userindex < userList.length; userindex++) {
                                        let element2 = userList[userindex];
                                        let consumerRate_one_employee = await formDataService.getgetTravelFrequency_oneemployeeDate1ToDate2(element2, date1, date2);
                                        consumerRate_area+=consumerRate_one_employee;
                                    }
                                    let rate:any=0;
                                    if(userList.length>0){
                                        rate=(consumerRate_area/userList.length);
                                    }else{
                                        rate=0
                                    }
                                    let a=(rate.toFixed(2)-0)*100;
                                    dataCache[element]={
                                        areaName:element,
                                        travelFrequency:a.toFixed(0)
                                    }
                                }
                            }
                        }
                        if (comment == '省份') {
                            let provinceNameList:any[]=[]
                            if(groupName.includes('模块')){
                                if(son_province_list.length>0){
                                    for (let proindex = 0; proindex < son_province_list.length; proindex++) {
                                        let element2 = son_province_list[proindex];
                                        if(element2){
                                            provinceNameList.push(element2)
                                        }
                                    }
                                }
                            }else if(groupName.includes('全国')){
                                provinceNameList=await visitMonthPlanModel.distinct("provinceName",{comment:1,"$and":[{provinceName:{$ne:null}},{provinceName:{$ne:''}}]})
                            }
                            if(provinceNameList.length>0){
                                for (let sonDeptindex = 0;  sonDeptindex< provinceNameList.length; sonDeptindex++) {
                                    let pro = provinceNameList[sonDeptindex];
                                    if (!dataCache[pro]) {
                                        dataCache[pro] = {
                                            areaName:pro,
                                            travelFrequency:''
                                        }
                                    }
                                    userList = await visitMonthPlanModel.distinct("userId", {provinceName:pro, comment: 1, deptId: { $in:son_list2 }, userId: { $ne: '' } }); 
                                    let consumerRate_area=0;//出差频率
                                    for (let userindex = 0; userindex < userList.length; userindex++) {
                                        let element2 = userList[userindex];
                                        let consumerRate_one_employee = await formDataService.getgetTravelFrequency_oneemployeeDate1ToDate2(element2, date1, date2);
                                        consumerRate_area+=consumerRate_one_employee;
                                    }
                                    let rate:any=0;
                                    if(userList.length>0){
                                        rate=(consumerRate_area/userList.length);
                                    }else{
                                        rate=0
                                    }
                                    let a=(rate.toFixed(2)-0)*100
                                    dataCache[pro]={
                                        areaName:pro,
                                        travelFrequency:a.toFixed(0)
                                    }
                                }
                            } 
                        }        
                    }
                }
            }
        }
        for (const key in dataCache) { //移植dataCache当中的属性到list当中
            if (dataCache.hasOwnProperty(key)) {
                const element = dataCache[key];
                list.push(element)
            }
        }
        if(list.length==0){
            list.push({
                areaName:'',
                travelFrequency:0
            })
        }
        console.log(list,'list_list_list_list_list_list_list_list_出差频率')
        return list;
    }

}
let reportForm = new ReportFormService();
export default reportForm;